package com.hui.admin.controller;

import com.hui.admin.annotation.Log;
import com.hui.admin.common.result.Result;
import com.hui.admin.common.result.Results;
import com.hui.admin.dto.request.AnnouncementDeleteBatchRequestDTO;
import com.hui.admin.dto.request.AnnouncementExportRequestDTO;
import com.hui.admin.dto.request.AnnouncementPageRequestDTO;
import com.hui.admin.dto.request.AnnouncementSaveRequestDTO;
import com.hui.admin.dto.response.AnnouncementPageResponseDTO;
import com.hui.admin.dto.response.AnnouncementResponseDTO;
import com.hui.admin.service.impl.AnnouncementServiceImpl;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 公告控制层
 */
@Slf4j
@RestController
@RequestMapping("/api/announcement")
@RequiredArgsConstructor
public class AnnouncementController {

    private final AnnouncementServiceImpl announcementService;

    /**
     * 新增公告
     */
    @Log("新增公告")
    @PostMapping
    public Result<Void> createAnnouncement(@RequestBody AnnouncementSaveRequestDTO requestDTO) {
        announcementService.createAnnouncement(requestDTO);
        return Results.success();
    }

    /**
     * 删除公告
     */
    @Log("删除公告")
    @DeleteMapping("/{id}")
    public Result<Void> deleteAnnouncement(@PathVariable Long id) {
        announcementService.deleteAnnouncement(id);
        return Results.success();
    }

    /**
     * 批量删除公告
     */
    @Log("批量删除公告")
    @DeleteMapping("/batch")
    public Result<Void> deleteAnnouncements(@RequestBody AnnouncementDeleteBatchRequestDTO requestDTO) {
        announcementService.deleteAnnouncements(requestDTO);
        return Results.success();
    }

    /**
     * 修改公告
     */
    @Log("修改公告")
    @PutMapping
    public Result<Void> updateAnnouncement(@RequestBody AnnouncementSaveRequestDTO requestDTO) {
        announcementService.updateAnnouncement(requestDTO);
        return Results.success();
    }

    /**
     * 修改公告状态
     */
    @Log("修改公告状态")
    @PutMapping("/status")
    public Result<Void> updateAnnouncementStatus(@RequestParam Long id, @RequestParam Integer status) {
        announcementService.updateAnnouncementStatus(id, status);
        return Results.success();
    }

    /**
     * 修改公告置顶
     */
    @Log("修改公告置顶")
    @PutMapping("/top")
    public Result<Void> updateAnnouncementIsTop(@RequestParam Long id, @RequestParam Integer isTop) {
        announcementService.updateAnnouncementIsTop(id, isTop);
        return Results.success();
    }

    /**
     * 分页查询
     */
    @GetMapping
    public Result<AnnouncementPageResponseDTO> getAnnouncementPage(AnnouncementPageRequestDTO requestDTO) {
        return Results.success(announcementService.getAnnouncementPage(requestDTO));
    }

    /**
     *
     */
    @GetMapping("/list")
    public Result<List<AnnouncementResponseDTO>> getAnnouncementList() {
        return Results.success(announcementService.getAnnouncementList());
    }

    /**
     * 导出公告数据
     */
    @Log("导出公告数据")
    @PostMapping("/export")
    public void exportAnnouncements(HttpServletResponse response,
                            @RequestBody AnnouncementExportRequestDTO requestDTO) {
        announcementService.exportAnnouncements(response, requestDTO);
    }

    /**
     * 导入公告数据
     */
    @Log("导入公告数据")
    @PostMapping("/import")
    public Result<Void> importAnnouncements(@RequestParam("file") MultipartFile file) {
        announcementService.importAnnouncements(file);
        return Results.success();
    }
}
